<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Languages extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('language');
	}
	public function ajax_language_get(){
		$languageEntity =  new CI_Language_entity();
		$language = $languageEntity->init('id',$_REQUEST['id']);
		echo json_encode($language);
	}
	public function ajax_language_list(){		
		$languageEntity =  new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$data['languageCodeList'] = array(	'ar' => 	'Arabic',
											'bg' 	=>'Bulgarian',
											'ca' 	=>'Catalan',
											'zh-CHS' 	=>'Chinese (Simplified)',
											'zh-CHT' 	=>'Chinese (Traditional)',
											'cs' 	=>'Czech',
											'da' 	=>'Danish',
											'nl' 	=>'Dutch',
											'en' 	=>'English',
											'et' 	=>'Estonian',
											'fi' 	=>'Finnish',
											'fr' 	=>'French',
											'de' 	=>'German',
											'el' 	=>'Greek',
											'ht' 	=>'Haitian Creole',
											'he' 	=>'Hebrew',
											'hi' 	=>'Hindi',
											'hu' 	=>'Hungarian',
											'id' 	=>'Indonesian',
											'it' 	=>'Italian',
											'ja' 	=>'Japanese',
											'ko' 	=>'Korean',
											'lv' 	=>'Latvian',
											'lt' 	=>'Lithuanian',
											'no' 	=>'Norwegian',
											'pl' 	=>'Polish',
											'pt' 	=>'Portuguese',
											'ro' 	=>'Romanian',
											'ru' 	=>'Russian',
											'sk'	=>'Slovak',
											'sl' 	=>'Slovenian',
											'es' 	=>'Spanish',
											'sv' 	=>'Swedish',
											'th' 	=>'Thai',
											'tr' 	=>'Turkish',
											'uk' 	=>'Ukrainian',
											'vi' 	=>'Vietnamese'
		);
		
		$this->load->view('admin/languages/index', $data);
	}
	public function ajax_language_save(){
		$result = array();
		$languageEntity =  new CI_Language_entity();
		if($_REQUEST['language'] == null || $_REQUEST['language'] == ""){
			$result['result'] = false;
			$result['message'] = lang('escort_language_require');
		}else{
			if($this->_check_language(0, $_REQUEST['language'])){
				$result['result'] = false;
				$result['message'] = lang('escort_language_existed');
			}else{
				$languageEntity->setLanguage($_REQUEST['language']);
				$languageEntity->setCode($_REQUEST['language_code']);
				$id = $languageEntity->save($languageEntity);
				$result['result'] = true;
				$result['message'] =lang('escort_language_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_language_update(){
		$result = array();
		$languageEntity =  new CI_Language_entity();
		if($_REQUEST['language'] == null || $_REQUEST['language'] == ""){
			$result['result'] = false;
			$result['message'] = lang('escort_language_require');
		}else{
			if($this->_check_language($_REQUEST['id'], $_REQUEST['language'])){
				$result['result'] = false;
				$result['message'] = lang('escort_language_existed');
			}else{
				$languageEntity->setLanguage($_REQUEST['language']);
				$languageEntity->setCode($_REQUEST['language_code']);
				$languageEntity->setId($_REQUEST['id']);
				$languageEntity->update();
				$result['result'] = true;
				$result['message'] = lang('escort_language_save_success');
			}
		}
		echo json_encode($result);
	}
	public function ajax_language_delete(){
		$result = array();
		$languageEntity =  new CI_Language_entity();
		$languageEntity->delete($_REQUEST['id']);
		$result['message'] = lang('escort_language_delete_success');
		echo json_encode($result);
	}
	function _check_language($id, $name){
		$ci = &get_instance();
		return $ci->language_entity->checkExistedName($id, $name);
	}
}
?>